This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Load the required libraries. If you don’t have them installed, please do by running install.packages()
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(stringr)
library(reshape2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(readr)
Load the NMR binned csv. Just adapt the path to location of your file. You can use autocompletion using the tab key
Binning_Fusarium_sh1 <- read_csv("../../data/Binning_Fusarium_matz_center_named.csv")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## .default = col_double()
## )
## ℹ Use `spec()` for the full column specifications.
Lets have a look at the first rows of this file
head(Binning_Fusarium_sh1)
OK. Be sure to have ppm on the columns and fraction numbers as rows. Now we transform the dataframe as a matrix
DTz <- as.matrix(data.frame(Binning_Fusarium_sh1))
Lets have a look at the structure of the file
str(DTz)
## num [1:135, 1:1603] 1 2 3 4 5 6 7 8 9 10 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:1603] "X." "X.1.00831" "X.0.998305" "X.0.988305" ...
Now we’ll remove the row indexes
DTz <- DTz[,-1]
And we set the matrix row and colnames according to the ones of the df
colnames(DTz) <- colnames(Binning_Fusarium_sh1)[-1]
rownames(DTz) <- rownames(Binning_Fusarium_sh1)
Let’s transform these data in the long form
mtrx.melt <- melt(DTz, id.vars = c('sample', 'ppm'), measure.vars = 'int')
names(mtrx.melt) <- c('sample', 'ppm', 'int')
Now we can plot a quick 3Dplot to have an overview of the data
p <- plot_ly(z = ~DTz) %>% add_surface()
p
OK so now we want to remove the annoying signals corresponding to the solvents. Here we want to get rid of the signals of DMSO at 2.5 ppm so we can subset the df and remove columns starting with this shift
# we turn the matrix as a df
DTz_df <- as.data.frame(DTz)
# and remove columns starting with the chemical shift we want to avoid, here DMSO signals a 2.5 ppm
DTz_df_sub <- DTz_df %>%
select(!starts_with(c('2.4', '2.5')))
# we turn this df back as a numerical matrix
DTz_mat_sub <- as.matrix(DTz_df_sub)
Now lets plot again …. Does it looks better ?
p <- plot_ly(z = ~DTz_mat_sub) %>% add_surface()
p
We now turn this as a melted matrix
mtrx.melt_sub <- melt(DTz_mat_sub, id.vars = c('sample', 'ppm'), measure.vars = 'int')
names(mtrx.melt_sub) <- c('sample', 'ppm', 'int')
Now that you have the cleaned data object lets have a look at the 2d map. Be patient, this one is longer to plot.
p <- plot_ly(mtrx.melt_sub, x = ~sample, y = ~ppm, z = ~int, type = "contour",
colors = 'YlOrRd',
autocontour = F,
contours = list(
start = 10000,
end = 1200000,
size = 5000
)
)
p
If you want to plot the map with ppm on the x-axis just reverse the axis order. Play with start value (to fix the noise) and size value to fix the contour space. Change color if you wish by changing the color field. For more info on 2d contour plot with plotly check https://plot.ly/r/contour-plots/
p <- plot_ly(mtrx.melt_sub, x = ~ppm, y = ~sample, z = ~int, type = "contour",
autocontour = F,
colors = 'YlOrRd',
contours = list(
start = 10000,
end = 7470000,
size = 20000
)
) %>% layout(xaxis = list(autorange = "reversed"))
p
htmlwidgets::saveWidget(as_widget(p), "2dNMR.html")